var strF = $('#buy').val();
var teacherName = $('.briefIntro').find('.up').children().eq(0).html();
var defaultChannel = '非常规渠道';
var channelWays = {
  "9": "阿财-选股牛人",
  "10": "理财晚餐-选股牛人",
  "11": "学堂-选股牛人",
  "12": "第一财经-选股牛人",
  "13": "天财牛人-选股牛人",
  "14":"天生阿财-选股牛人"
};
var way = "阿财-" + (channelWays[strF] || defaultChannel) + "-" + teacherName;

/*购买行为*/
var applyS = document.getElementById("applyS"),
    footer = document.getElementById("fixed");
function show() {
  footer.style.display = "none";
  applyS.style.display = "block";
}
function hide(e) {
  if (e.target == this || e.target.className == "words" || e.target.className == "close") {
    applyS.style.display = "none";
    footer.style.display = "block";
  }
}
applyS.addEventListener('click', hide);
footer.addEventListener('click', show);

function changeBuyWay() {
  var img = $('#applyS').find('.zxt_success').find('img');
  var baseUrl = img.attr('src');
  var newUrl;
  if (channelWays[strF]) {
    newUrl = baseUrl.slice(0, -5) + strF + '.png';
    img.attr('src', newUrl);
  }
}


$(function () {
  $("#btn").click(function () {
    if ($("#btn").html() == "获取验证码") {
      var phone = $("#phone").val();
      var re = /^1[3|4|5|7|8][0-9]\d{8}$/;
      if (!re.test(phone)) {
        alert('手机号格式错误!');
        return false;
      }
      countDown();
    }
  });
  function countDown() {
    var i = 60;
    getValidateCode();
    var clr = setInterval(function () {
      if (i-- > 0) {
        $("#btn").html(i + "秒后重新获取");
      }
      else {
        $("#btn").html("获取验证码");
        clearInterval(clr);
      }
    }, 1000);
  };

  changeBuyWay();
});


/*购买判定*/
function register() {
  var phone = $("#phone").val();
  var code = $("#code").val();
  var reCode = $("#reCode").val();
  var re = /^1[3|4|5|7|8][0-9]\d{8}$/;
  if (!re.test(phone)) {
    alert("亲！手机号格式错误 ~");
    return false;
  }
  if (code != reCode) {
    alert("亲！验证码错误 ~");
    return false;
  }
  $.ajax({
    url: ctx + 'toZeroOrder/ZeroToOrder',
    type: "post",
    data: {
      "phone": phone,
      "validateCode": "code",
      "way": way
    },
    async: false,
    success: function (datas) {
      if (datas.flag) {
        $("#regForm").hide();
        $(".zxt_success").show();
      }
      else {
        alert("亲！不可重复报名 ~");
      }
    },
    error: function () {
      alert("连接失败，请稍后再试！");
    }
  });
}
function getValidateCode() {
  var phone = $("#phone").val();
  $.ajax({
    url: ctx + 'toZeroOrder/sendValidateCode2',
    type: "post",
    data: {"phone": phone},
    async: false,
    success: function (datas) {
      if (datas.flag) {
        $("#reCode").val(datas.data);
        alert("发送成功");
      }
    },
    error: function () {
      alert("连接失败，请稍后再试！");
    }
  });
}



